package com.example.tvfoucssustom;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.example.tvfoucssustom.data.Contans;
import com.example.tvfoucssustom.fragment.MessageFragment;
import com.example.tvfoucssustom.fragment.MusicFragment;
import com.example.tvfoucssustom.fragment.PhotoFragment;
import com.example.tvfoucssustom.fragment.SettingFragment;
import com.example.tvfoucssustom.fragment.VideoFragment;
import com.example.tvfoucssustom.fragment.WoxiuFragment;
import com.example.tvfoucssustom.util.DebugLog;

import static android.view.KeyEvent.KEYCODE_DPAD_UP;
import static com.example.tvfoucssustom.data.Contans.HAND_FOUCS_DOWN;
import static com.example.tvfoucssustom.data.Contans.HAND_MEMORY_FOUCS_DEFULT;
import static com.example.tvfoucssustom.data.Contans.TAB_MEMORY_FOUCS_DEFULT;
import static com.example.tvfoucssustom.data.Contans.TAB_MESSAGE_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_MUSIC_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_PHOTO_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_SET_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_VIDEO_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_WOXIU_POSTION;
import static com.example.tvfoucssustom.util.DebugLog.LOG;

public class HomeCustomActivity extends AppCompatActivity implements TvCustomLayout.FoucsFragmentListener {


    private TvCustomLayout tvWo;
    private TvCustomLayout tvPhoto;
    private TvCustomLayout tvVideo;
    private TvCustomLayout tvMess;
    private TvCustomLayout tvSet;
    private Fragment currentFragment;
    private WoxiuFragment woxiuFragment;
    private MusicFragment musicFragment;
    private PhotoFragment photoFragment;
    private VideoFragment videoFragment;
    private MessageFragment messageFragment;
    private SettingFragment settingFragment;
    private TvCustomLayout tvMusic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_tab);
        tvWo = findViewById(R.id.wo);
        tvPhoto = findViewById(R.id.photo);
        tvVideo = findViewById(R.id.video);
        tvMess = findViewById(R.id.mess);
        tvSet = findViewById(R.id.set);
        tvMusic = findViewById(R.id.music);
        tvPhoto.requestFocus();
        initFragment();

        initEvent();

    }

    private void initFragment() {
        woxiuFragment = new WoxiuFragment();
        musicFragment = new MusicFragment();
        photoFragment = new PhotoFragment();
        videoFragment = new VideoFragment();
        messageFragment = new MessageFragment();
        settingFragment = new SettingFragment();
        //首次进来记忆
        selectFragment(photoFragment, 1);
        Contans.TAB_MEMORY_FOUCS = TAB_PHOTO_POSTION;
    }

    private void initEvent() {
        tvWo.setFoucsFragmentListener(this);
        tvPhoto.setFoucsFragmentListener(this);
        tvVideo.setFoucsFragmentListener(this);
        tvMess.setFoucsFragmentListener(this);
        tvSet.setFoucsFragmentListener(this);
        tvMusic.setFoucsFragmentListener(this);

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        DebugLog.d(DebugLog.LOG + "---keyCode----  " + keyCode + " Contans.TAB_MEMORY_FOUCS " + Contans.TAB_MEMORY_FOUCS);
        switch (keyCode) {
            case KEYCODE_DPAD_UP:
                if (Contans.TAB_MEMORY_FOUCS == TAB_WOXIU_POSTION) {
                    tvWo.requestFocus();
                    Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
                    return true;
                }
                if (Contans.TAB_MEMORY_FOUCS == TAB_PHOTO_POSTION) {
                    DebugLog.d(DebugLog.LOG+" photoFragment.isBefore() "+photoFragment.isBefore());
                    if (photoFragment.isBefore()) {

                        tvPhoto.requestFocus();
                        Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
                        return true;
                    }
                }
                if (Contans.TAB_MEMORY_FOUCS == TAB_MUSIC_POSTION) {
                    tvMusic.requestFocus();
                    Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
                    return true;
                }
                if (Contans.TAB_MEMORY_FOUCS == TAB_VIDEO_POSTION) {
                    tvVideo.requestFocus();
                    Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
                    return true;
                }
                if (Contans.TAB_MEMORY_FOUCS == TAB_MESSAGE_POSTION) {
                    tvMess.requestFocus();
                    Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
                    return true;
                }
                if (Contans.TAB_MEMORY_FOUCS == TAB_SET_POSTION) {
                    tvSet.requestFocus();
                    Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
                    return true;
                }
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:

                Contans.HAND_MEMORY_FOUCS = HAND_FOUCS_DOWN;
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:

                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:

                break;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void selectFragment(Fragment fragment, int tag) {
        FragmentTransaction mFt = getSupportFragmentManager().beginTransaction();
        currentFragment = getSupportFragmentManager().findFragmentByTag(tag + "");
        if (currentFragment == null) {
            currentFragment = fragment;
            mFt.add(R.id.home_tab_container, currentFragment, String.valueOf(tag));
        } else {
            mFt.show(currentFragment);
        }
        mFt.commit();

    }

    private void unSelectFragment() {
        if (currentFragment != null) {
            FragmentTransaction mFt = getSupportFragmentManager().beginTransaction();
            mFt.hide(currentFragment);
            mFt.commit();
        }

    }

    @Override
    public void seleteFoucsTab(int tag) {
        DebugLog.d(DebugLog.LOG + "---seleteFoucsTab  " + tag);
        Contans.TAB_MEMORY_FOUCS = tag;
        switch (tag) {
            case 0:
                selectFragment(woxiuFragment, 0);
                break;
            case 1:

                selectFragment(photoFragment, 1);
                break;
            case 2:
                selectFragment(musicFragment, 2);
                break;
            case 3:
                selectFragment(videoFragment, 3);
                break;
            case 4:
                selectFragment(messageFragment, 4);
                break;
            case 5:
                selectFragment(settingFragment, 5);
                break;

        }
    }

    @Override
    public void unSeleteFoucsTab(int tag) {
        DebugLog.d(LOG + " ---- unSeleteFoucsTab--- " + tag);
        unSelectFragment();
    }

    @Override
    public void setActivatedTab(Integer tag) {
        //保持高亮的标题
        Contans.TAB_MEMORY_FOUCS = tag;
    }

    @Override
    protected void onStop() {
        super.onStop();
        Contans.TAB_MEMORY_FOUCS = TAB_MEMORY_FOUCS_DEFULT;
        Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
    }
}
